//
//  KABMallTitleView.m
//  kabu
//
//  Created by XiangDongMBP on 2017/3/24.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABMallTitleView.h"

@interface KABMallTitleView () <UITextFieldDelegate>

@end

@implementation KABMallTitleView

- (instancetype)init {
    self = [super init];
    if (self) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"KABMallTitleView" owner:self options:nil] lastObject];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 4.0;
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTapped:)];
//    [self addGestureRecognizer:tapGesture];
    
    [self.searchBtn addTarget:self action:@selector(searchBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.textField addTarget:self action:@selector(textFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.textField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
}

//- (void)bgViewTapped:(UIGestureRecognizer *)gesture {
//    if ([self.delegate respondsToSelector:@selector(kABMallTitleView:bgViewTapped:)]) {
//        [self.delegate kABMallTitleView:self bgViewTapped:gesture.view];
//    }
//}

- (void)textFieldEditingChanged:(UITextField *)textField {
    NSLog(@"输入字符 %@", textField.text);
    if ([self.delegate respondsToSelector:@selector(kABMallTitleView:textFieldChanged:)]) {
        [self.delegate kABMallTitleView:self textFieldChanged:textField];
    }
}

- (void)textFieldDidEndOnExit:(UITextField *)textField {
    [textField resignFirstResponder];
    NSLog(@"输入%@", textField);
    if ([self.delegate respondsToSelector:@selector(kABMallTitleView:searchBtnTapped:)]) {
        [self.delegate kABMallTitleView:self searchBtnTapped:self.searchBtn];
    }
}

- (void)searchBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABMallTitleView:searchBtnTapped:)]) {
        [self.delegate kABMallTitleView:self searchBtnTapped:btn];
    }
}

- (NSString *)inputText {
    return self.textField.text;
}

- (void)setTextFieldPlaceholder:(NSString *)str {
    if (!str) {
        return;
    }
    
    NSDictionary *dic = @{NSForegroundColorAttributeName : self.placeholderColor ? self.placeholderColor : [UIColor whiteColor]};
    self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:str attributes:dic];
}

- (void)setTextFieldText:(NSString *)str {
    self.textField.text = str;
}

#pragma mark - getters and setters
- (void)setBgColor:(UIColor *)bgColor {
    _bgColor = bgColor;
    self.backgroundColor = bgColor;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    self.textField.textColor = textColor;
}
- (void)setTextFieldTintColor:(UIColor *)textFieldTintColor {
    _textFieldTintColor = textFieldTintColor;
    
    self.textField.tintColor = textFieldTintColor;
}
@end
